import com.zhao.bike.*;
import consts.BikeConstans;
import dao.BikeDao;
import impl.BikeDaoImpl;

import java.util.Scanner;

/**
 * @classname:SharedBikeMgr
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/3 22:47
 * @version: 0.1
 * @since:1.8
 */
public class SharedBikeMgr {

    SharedBikeCompany[] sharedBikeCompanies;
    Scanner input;
    BikeDao bikeDao;

    public void initial() {
        input = new Scanner(System.in);
        bikeDao = new BikeDaoImpl();
        SharedBike[] ofo = new OfoBike[5];
        SharedBike[] mobai = new MobaiBike[5];
        SharedBike[] halo = new HaloBike[5];

        for (int i = 1; i <= 5; i++) {
            ofo[i - 1] = new OfoBike(i, BikeConstans.OFO_BIKE_NAME + i, 1, null);
            mobai[i - 1] = new MobaiBike(i, BikeConstans.MOBAI_BIKE_NAME + i, 1, null);
            halo[i - 1] = new HaloBike(i, BikeConstans.HALO_BIKE_NAME + i, 1, null);
        }
        sharedBikeCompanies = new SharedBikeCompany[3];
        sharedBikeCompanies[0] = new SharedBikeCompany(1, BikeConstans.OFO_COMPANY_NAME, ofo, 5, 100);
        sharedBikeCompanies[1] = new SharedBikeCompany(2, BikeConstans.MOBAI_COMPANY_NAME, mobai, 5, 200);
        sharedBikeCompanies[2] = new SharedBikeCompany(3, BikeConstans.HALO_COMPANY_NAME, halo, 5, 300);
    }

    public void startMenu() {
        System.out.println("              欢迎使用迷你共享单车");
        System.out.println("*********************************************");
        System.out.println("              1.投放Bike");
        System.out.println("              2.查看Bike");
        System.out.println("              3.删除Bike");
        System.out.println("              4.借出Bike");
        System.out.println("              5.归还Bike");
        System.out.println("              6.Bike排行榜");
        System.out.println("              7.退出");
        System.out.println("*********************************************");
        System.out.println("请选择:");
        int choice = input.nextInt();
        switch (choice) {
            case 1:
                addModule();
                break;
            case 2:
                bikeDao.selectBike(sharedBikeCompanies);
                break;
            case 3:
                deleteModule();
                break;
            case 4:
                borrowModule();
                break;
            case 5:
                retrunModule();
                break;
            case 6:
                System.out.println("---->排行榜");
                bikeDao.rankBike(sharedBikeCompanies);
                break;
            case 7:
                return;
        }
        returnMenu();
    }

    private void retrunModule() {
        System.out.println("--->归还");
        int i = chooseCompany();
        bikeDao.returnBike(sharedBikeCompanies[i - 1], input);
    }

    private void borrowModule() {
        System.out.println("----->借车");
        int i = chooseCompany();
        bikeDao.borrowBike(sharedBikeCompanies[i - 1], input);
    }

    private void deleteModule() {
        System.out.println("----->删除");
        int i = chooseCompany();
        bikeDao.deleteBike(sharedBikeCompanies[i - 1], input);
    }

    private void addModule() {
        System.out.println("----->投放");
        int i = chooseCompany();
        System.out.println("请输入要投放的数量");
        int addNum = input.nextInt();
        bikeDao.addBike(addNum, sharedBikeCompanies[i - 1]);

    }

    public int chooseCompany() {
        System.out.println("请选择要操作的公司");
        System.out.println("1:" + BikeConstans.OFO_COMPANY_NAME);
        System.out.println("2:" + BikeConstans.MOBAI_COMPANY_NAME);
        System.out.println("3:" + BikeConstans.HALO_COMPANY_NAME);
        int choose = input.nextByte();
        return choose;
    }


    public void returnMenu() {
        System.out.println("输入0返回");
        int num = input.nextInt();
        startMenu();
    }


    public SharedBikeMgr() {
        initial();
    }


}
